package io.github.talelin.latticy.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.github.talelin.autoconfigure.exception.AuthenticationException;
import io.github.talelin.autoconfigure.exception.NotFoundException;
import io.github.talelin.latticy.common.CommonUtil;
import io.github.talelin.latticy.common.LocalUser;
import io.github.talelin.latticy.common.UserUtil;
import io.github.talelin.latticy.dto.ProjectDTO;
import io.github.talelin.latticy.entity.Project;
import io.github.talelin.latticy.mapper.ProjectMapper;
import io.github.talelin.latticy.model.GroupDO;
import io.github.talelin.latticy.model.ProjectDO;
import io.github.talelin.latticy.model.UserDO;
import io.github.talelin.latticy.repository.ProjectRepository;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class ProjectService extends ServiceImpl<ProjectMapper, ProjectDO> {

    @Autowired
    private ProjectRepository projectRepository;

    @Autowired
    private UserUtil userUtil;

    public void edit(Long id, ProjectDTO dto) {
        Project project = this.get(id);
        userUtil.userAuthority();
        BeanUtils.copyProperties(dto, project);
        projectRepository.save(project);
    }

    public void create(ProjectDTO dto) {
        Project project = new Project();
        userUtil.userAuthority();
        Date now = new Date();
        project.setUserId(UserUtil.getCurrentUserId());
        project.setCreateTime(now);
        BeanUtils.copyProperties(dto, project);
        projectRepository.save(project);
    }

    public void delete(Long id) {
        Project project = this.get(id);
        Date date = Calendar.getInstance().getTime();
        project.setDeleteTime(date);
        projectRepository.save(project);
    }

    public Project get(Long id) {
        Project project = projectRepository.findFirstById(id);
        if (project == null) {
            throw new NotFoundException();
        }
        return project;
    }

    public Page<Project> getAll(Boolean isSelect, String type, String status, int page, int size) {
        List<String> typeList = new ArrayList<>();
        List<String> statusList = new ArrayList<>();
        if (type.equals("1") || type.equals("2")) {
            typeList.add(type);
        } else {
            typeList = Arrays.asList("1", "2");
        }
        if (status.equals("1") || status.equals("2") || status.equals("3")) {
            statusList.add(status);
        } else  {
            statusList = Arrays.asList("1", "2", "3");
        }

        Pageable pageable = PageRequest.of(page, size);

        if (isSelect) {
            //选择项目
            return projectRepository.findProjectsByTypeAndStatusOrderByCreateTimeAndStatus(typeList, statusList, pageable);
        } else {
            return projectRepository.findProjectsByTypeAndStatusOrderByCreateTime(typeList, statusList, pageable);
        }
    }

    /******************** 后台 ********************************/
    public void add(ProjectDTO dto) {
        ProjectDO projectDO = new ProjectDO();
        UserDO userDO = LocalUser.getLocalUser();
        projectDO.setUserId(userDO.getId());
        BeanUtils.copyProperties(dto, projectDO);
        this.save(projectDO);
    }

    public void updated(Long id, ProjectDTO dto) {
        ProjectDO projectDO = this.getProject(id);
        BeanUtils.copyProperties(dto, projectDO);
        this.updateById(projectDO);
    }

    public ProjectDO getProject(Long id) {
        ProjectDO projectDO = this.getById(id);
        if (projectDO == null) {
            throw new NotFoundException();
        }
        return projectDO;
    }
}
